package com.wan37.logic.backpack.item;

import com.wan37.logic.component.LogicWorkerHandlerCenter;
import com.wan37.logic.domain.Result;
import com.wan37.logic.handler.server.SuperMoveHandler;
import com.wan37.logic.service.cache.RoleServiceCache;
import com.wan37.logic.service.cache.SceneServiceCache;
import com.wan37.logic.service.cache.UserServiceCache;
import com.wan37.protobuf.constant.CommonBackpack;
import com.wan37.protobuf.message.client2server.game.GameBackpack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 传送符使用逻辑
 *
 * @author : luoyong
 * @date : 2020-07-06 12:57
 **/
@Component("portal")
public class PortalItem implements Item {
    @Autowired
    private SceneServiceCache sceneServiceCache;
    @Autowired
    private ItemManager itemManager;
    @Autowired
    private LogicWorkerHandlerCenter logicWorkerHandlerCenter;

    @Override
    public Result execute(UserServiceCache.User user,
                          RoleServiceCache.Role role, GameBackpack.CGameItemMsg cGameItemMsg) {
        int count = user.getItemVOMap().get(cGameItemMsg.getBackpackId()).getCount();
        if (count >= 0) {
            itemManager.reduceItem(user, user.getItemVOMap().get(cGameItemMsg.getBackpackId()), 1);
            logicWorkerHandlerCenter.distributeWork(user.getSceneId(),
                    new SuperMoveHandler(user.getSceneId(), sceneServiceCache.sceneNameToIdMap
                            .get(cGameItemMsg.getAttachment()), user.getUserVO().getUserId(),
                            cGameItemMsg.getAttachment()));
            return null;
        } else {
            return new Result(CommonBackpack.ITEM_ERROR, "传送符使用失败，原因是物品余额为0！！");
        }
    }
}
